package com.greet.service.impl;

import com.greet.properties.AliOssProperties;
import com.greet.service.CommonService;
import com.greet.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private AliOssProperties aliOssProperties;
    @Override
    public String upload(MultipartFile file) throws IOException {
        AliOssUtil aliOssUtil = new AliOssUtil(aliOssProperties.getEndpoint(),
                aliOssProperties.getAccessKeyId(),
                aliOssProperties.getAccessKeySecret(),
                aliOssProperties.getBucketName());
        String originalFileName =  file.getOriginalFilename();
        int index = originalFileName.lastIndexOf(".");
        String extname = originalFileName.substring(index);

        //组合uuid与文件拓展名
        String newFileName = UUID.randomUUID().toString() + extname;
        String ulr = aliOssUtil.upload(file.getBytes(), newFileName);
        return ulr;
    }
}
